package com.dhcc.bpm.alarm.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhcc.bpm.alarm.service.IAlarmTaskService;
import com.dhcc.bpm.alarm.service.impl.TaskAsyncService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("alarm/task")
public class AlarmTaskController {
	
	@Autowired
	IAlarmTaskService alarmTaskService;
	
	@Autowired
	TaskAsyncService taskAsyncService;
	
	
	/** 定时插入告警记录数量-按分钟  **/
	@PostMapping("/insertRecordMinutes")
	public JSONObject insertRecordMinutes(@RequestBody JSONObject taskParam) {
		MDC.put("traceNo", taskParam.getString("traceNo"));
		log.info("按分钟插入告警记录数量 start-：", taskParam);
		
		JSONObject result = new JSONObject();
		try {
			JSONObject  executeRs = alarmTaskService.insertRecordMinutes(taskParam);
			taskAsyncService.resultNotice(taskParam, "00", executeRs.getString("msg"));
			
			result.put("code", "0000");
			result.put("msg", "同步成功");
		} catch (Exception e) {
			log.info("按分钟统计通道数量-异常：{}", e); 
			taskAsyncService.resultNotice(taskParam, "03", "数据同步失败");
			
			result.put("code", "1111");
			result.put("msg", "同步失败");
		}
		
		MDC.remove("traceNo");
		return result;
	}
    
    
    /** 定时统计通道数量-按天 **/
	@PostMapping("/insertRecordDays")
	public JSONObject insertRecordDays(@RequestBody JSONObject taskParam) {
		MDC.put("traceNo", taskParam.getString("traceNo"));
		log.info("按天统计插入告警记录数量 start-：", taskParam);
		JSONObject result = new JSONObject();
		try {
			JSONObject  executeRs = alarmTaskService.insertRecordDays(taskParam);
			taskAsyncService.resultNotice(taskParam, "00", executeRs.getString("msg"));
			
			result.put("code", "0000");
			result.put("msg", "同步成功");
		} catch (Exception e) {
			log.info("按天统计插入告警记录数量-异常：{}", e); 
			taskAsyncService.resultNotice(taskParam, "03", "数据同步失败");
			
			result.put("code", "1111");
			result.put("msg", "同步失败");
		}
		
		log.info("按天统计插入告警记录数量 end-：", result);
		MDC.remove("traceNo");
		return result;
	}
	
	
	
	

}
